package com.doit.day01.op

//reduce   reduceLeft  reduceRight
object ReduceDemo {
  def main(args: Array[String]): Unit = {

    val list: List[Int] = List(1, 2, 3, 4, 5, 6, 7)


/*    val res1: Int = list.reduce((a: Int, b: Int) => a + b)
    println(res1)


    val res2: Int = list.reduceRight((a: Int, b: Int) => a + b)
    println(res2)*/


    //改成相减  1-2 = -1 -3 = -4 -4 = -8 -5 = -13 -6 -19 -7 = -26
    val res3: Int = list.reduce((a: Int, b: Int) => a - b)
    println(res3)//-26

    // 7 -6 = 1 -5  = -4 -4 = -8 -3 = -11 -2 = -13 -1 = -14   ==》 这个逻辑是错的
    //    6-  赋值给b  后面每次拿到得新元素赋值给a
    // 1-(  2-( 3-(4-( 5 -  (6-7))))) = 4

    val res4: Int = list.reduceRight((a: Int, b: Int) => a - b)
    println(res4)//4






  }

}
